<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

const userStore = useUserStore()
const router = useRouter()
const route = useRoute()

const formRef = ref(null)

const loginForm = ref({ username: 'admin', password: '111111' })

const loading = ref(false)

const login = async () => {
  loading.value = true
  try {
    await userStore.userLogin(loginForm.value)
    router.push(route.query.redirect || '/')
    ElNotification({
      type: 'success',
      message: '登陆成功',
      title: getTime() + '好，欢迎回来',
      duration: 2000,
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: error.message,
      duration: 2000,
    })
  }
}

const checkUserName = (rule, value, callback) => {
  // rule 为校验规则对象
  console.log(rule)
  // value 表单元素文本内容
  console.log(value)
  // 如果符合条件，通过callback放行，不符合条件，通过callback注入错误提示信息
  // if(/^[\u4e00-\u9fa5]{2,16}$/.test(value)) {
  //   callback()
  // } else {
  //   callback(new Error('用户名应为2-16位汉字'))
  // }
  if (/^\w{2,16}$/.test(value)) {
    callback()
  } else {
    callback(new Error('用户名应为2-16字母、数字或下划线组成'))
  }
}

const checkPassword = (rule, value, callback) => {
  // if(/^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{6,24}$/.test(value)) {
  //   callback()
  // } else {
  //   callback(new Error('密码由字母、数字、特殊字符，任意2种组成，6-24位'))
  // }
  if (/^\w{6,24}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码由字母、数字、下划线组成，6-24位'))
  }
}

const rules = ref({
  username: [
    // { required: true, message: '用户名不可为空', trigger: 'change' },
    // { min: 2, message: '用户名不可少于两位字符', trigger: 'change' },
    { validator: checkUserName, trigger: 'change' },
  ],
  password: [{ validator: checkPassword, trigger: 'change' }],
})
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="formRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>

          <el-form-item style="padding: 0 5%" prop="username">
            <el-input
              :prefix-icon="User"
              size="large"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>

          <el-form-item style="padding: 0 5%" prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              size="large"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>

          <el-form-item style="padding: 0 5%">
            <el-button
              :loading="loading"
              type="primary"
              class="login_btn"
              size="large"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="less" scoped>
.login_container {
  overflow-y: hidden;
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    .login_btn {
      width: 100%;
    }
  }
  h1 {
    color: #fff;
    font-size: 40px;
  }
  h2 {
    color: #fff;
    font-size: 20px;
    margin: 20px 0;
  }
}
</style>
